package cn.ly.security.controller;

import cn.ly.security.entity.Info;
import cn.ly.security.service.impl.InfoServiceImpl;
import cn.ly.security.util.DataPojo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

@RestController
@RequestMapping("/iot")
@CrossOrigin("*")
@Api(tags="第三方接口")
public class IotController {
    public static HashMap<Integer, Queue<Double>> map = new HashMap();

    @Autowired
    InfoServiceImpl infoService;

    @GetMapping("/getTemperature")
    public String getTemperature(@RequestBody HashMap<String, Object> hashMap) {
        Info info=new Info( );
        Object obj = hashMap.get("id");
        Integer id = Integer.valueOf(obj.toString());
        Object temperature = hashMap.get("temperature");
        Double aDouble = Double.valueOf(temperature.toString());
        info.setIId(id);

        Info byId = infoService.getById(id);
        if (aDouble <= 10) {


            info.setIStage( "良好");
            boolean update = infoService.save(info);
            System.out.println("良好");
        } else if (aDouble <= 20) {

            info.setIStage( "警告");
            boolean update = infoService.save(info);
            System.out.println("警告");
        } else if (aDouble <= 30) {
           info.setIStage("危险");
          info.setIStateCode(byId.getIStateCode() + 1);
            boolean update = infoService.save(info);
            System.out.println("危险");
        }
        Queue<Double> doubles;
        if ((doubles = map.get(id)) == null) {
            doubles = new LinkedList<>();
            doubles.offer(aDouble);
        } else {
            if (doubles.size() > 6) {
                doubles.poll();
            }
        }
        map.put(id, doubles);
        return null;
    }

    @GetMapping("/toFrontEnd")
    public DataPojo toFrontEnd() {
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(map);
        return new DataPojo("200", "success", objects);
    }
}
